package com.darco.darcoapp;

import com.darco.darcoapp.interfaces.IGeoLocatingActivity;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;

public class GeolocationHelper implements LocationListener {
	private Handler myEventHandler = new Handler(); //usato per reinstradare sul thread UI

	private float currentAccuracy;
	private float latitude  = 0.0f;
	private float longitude = 0.0f;
	private boolean gotLocation = false;
	private LocationManager    myLocationManager;
	private IGeoLocatingActivity myParentGeolocatingActivity;
	private Context myContext;
	
	public GeolocationHelper(IGeoLocatingActivity act){
		myParentGeolocatingActivity = act;
		myContext = (Activity)act;
	}
	
	public boolean isLocationEnabled(){
		if(myLocationManager == null)
			myLocationManager = (LocationManager)myContext.getSystemService(Context.LOCATION_SERVICE);
		
		return myLocationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) || myLocationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER );
	}
	
	public boolean startGeoLocating(){
		
		
		//setup the location manager
		if(myLocationManager == null)
			myLocationManager = (LocationManager)myContext.getSystemService(Context.LOCATION_SERVICE);
        
        if ( !myLocationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
	        return false;
	    }
         
        //setup a callback for when the GRPS/WiFi gets a lock and we receive data
        myLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        //setup a callback for when the GPS gets a lock and we receive data
        myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        
        return true;
	}
	
	//called when the location service reports a change in location
	public void onLocationChanged(Location location) {

		double lat = location.getLatitude();
		double lon = location.getLongitude();
		
		//store lat and long
		latitude = (float) lat;
		longitude = (float) lon;

		//now we have our location we can stop the service from sending updates
		//comment out this line if you want the service to continue updating the users location
		myLocationManager.removeUpdates(this);

		//change the flag to indicate we now have a location
		gotLocation = true;
		
		myParentGeolocatingActivity.onLocationObtained();
	}
	
	
	

	//called when the provider is disabled
	public void onProviderDisabled(String provider) {}
	//called when the provider is enabled
	public void onProviderEnabled(String provider) {}
	//called when the provider changes state
	public void onStatusChanged(String provider, int status, Bundle extras) {}


	public void killLocationServices(){
		myLocationManager.removeUpdates(this);
	}

	public float getLatitude() {
		return latitude;
	}

	public float getLongitude() {
		return longitude;
	}
}

